<template>
  <div class="app-cont">
    <!--这是 Header 区域 -->
    <div class="main">
      <div class="main-container">
        <div class="fenlei">
          <div class="idcar">
            <mt-field type="text" label="收货人" placeholder="请填写姓名" v-model="name">
              <div class="mint-field-other">&gt;</div>
            </mt-field>
            <mt-field label="联系电话" placeholder="请输入您的手机号码" v-model="number">
              <div class="mint-field-other">&gt;</div>
            </mt-field>
            <!--<mt-field label="所在地区" v-model="address" placeholder="请填写省市区">-->
              <!--<div class="mint-field-other">&gt;</div>-->
              <!--&lt;!&ndash;<v-distpicker class="addPick"></v-distpicker>&ndash;&gt;-->
            <!--</mt-field>-->

            <!-- Vue2 -->
            <div class="addPick" style="background: #ffffff;border-bottom: 1px solid #f4f4f4;">
              <span>所在地区</span>
              <!--<button @click="citySelect()">{{addr}}</button>-->
              <citySelect class="citySelect" ref="citySelect"></citySelect>
            </div>

            <textarea cols="5" class="addr_detail" placeholder="请填写详细地址，如街道、楼牌号等" v-model="street"></textarea>
            <div class="select" @click="selected()">
              <span :class="pretermission?'red':''">设为默认</span>
              <i :class="pretermission?'selected':''"></i>
            </div>
          </div>
          <div class="btn">
            <a href="javascript:;" class="btn-next" @click="NewCard">保存</a>
          </div>
        </div>
      </div>
    </div>
    <alertTip v-if="showAlert" @closeTip="go()" :alertText="alertText"></alertTip>
  </div>
</template>

<script>
  /* eslint-disable semi */
  import Vue from 'vue'
//  import RegionPicker from 'vue-region-picker'
//  import REGION_DATA from 'china-area-data'
//  Vue.use(RegionPicker, {
//    region: REGION_DATA
//  })
  import wx from 'weixin-js-sdk'
  import { wxHttpTool,wx_config, http_getParam, get_shopQrcoe,wx_share,wx_hideAllMenu } from '../components/httpUtil/httpUtil'
  import {msg_10046} from '../message/msg_10046'
import {relogin} from "../components/httpUtil/httpUtil";

  export default {
    name: '',
    data () {
      return {
//        showChose: false,
//        addr: '请选择省市区',
        name: '',
        number: '',
        address: '',
        street: '',
        pretermission: false,
        clickIndex: 0,
        showAlert:false,
        alertText:null
      }
    },
    methods: {
      selected () {
//        this.curId = this.length + 1;
        this.clickIndex ++;
        if (this.clickIndex % 2 !== 0) {
          this.pretermission = true;
//          this.length ++;
        } else {
          this.pretermission = false;
        }
      },
      alertTip(msg){
        this.showAlert = true;
        this.alertText = msg;
      },
      go(){
        this.showAlert = false;
      },
      NewCard () {
        let reg1 = /^1[34578]\d{9}$/;  // 手机号格式是否正确
        let reg2 = /\//; // 地址是否选取
        this.address = this.$refs.citySelect.addr;
        console.log(reg2.test(this.address))
//        alert('保存成功！' + this.pretermission)
        if (this.name !== '' && reg1.test(this.number) && reg2.test(this.address) && this.street !== '') {
          const jsonValue = JSON.stringify({
            'c': '10023',
            'p': {
              'userId': JSON.parse(window.localStorage.data).id, // 用户ID
//              'userId': 1009, // 用户ID
              'tokenId': token(),
              'name': this.name,
              'phone': this.number,
              'address': this.address,
              'street': this.street,
              'isUsing': this.pretermission
            }
          });
          this.$http.post(api + '/qqs.us', jsonValue).then((res) => {
            console.log(res);
            if (res.data.p.isTrue === true) {
              this.$router.push({
                name: 'addressManage',
                params: {}
              });
              this.alertTip('保存新建')
              this.go=function () {
                this.showAlert = false
              }
            } else {
              this.alertTip(res.data.p.errorMsg)
              if (res.data.p.errorCode === 90) {
                this.go= ()=> {
                  this.showAlert = false
                  let _this = this
                  relogin(_this)
                }
              }
            }
          }, (res) => {
            this.telephone = '';
            this.alertTip('网络错误');
            this.go = ()=>{
               this.showAlert = false;
            }
            // alert();
          });
        } else if (this.number !== '' && !reg1.test(this.number)) {
          this.alertTip('手机格式不正确');
          this.go = ()=>{
             this.showAlert = false;
          }
          // alert('手机格式不正确');
        } else {
          this.alertTip('请完整填写相关信息')
          this.go = ()=>{
             this.showAlert = false;
          }
          // alert('请完整填写相关信息')
        }
      }
    },
    created(){
      wxHttpTool.wxAuthor(this, function (varRet, varJson, varThis = this) {
        if (varRet == -1) {
          varThis.alertTip('网络错误');
          varThis.go = function () {
            varThis.showAlert = false;
          }

        }
        // 关注钱应该判断下是否已经关注过了
        msg_10046.send(varThis,msg_10046.entity(),function(varThis,res){
          varThis.wxlist=res.data.p.sign
          wx_config(varThis.wxlist);
          wx_hideAllMenu();
          wx.hideMenuItems({
            //禁用单独分享朋友圈QQ空间等功能
            menuList: ["menuItem:share:timeline","menuItem:share:QZone","menuItem:share:qq"] // 要隐藏的菜单项，所有menu项见附录3
          });
        })


      },this.type)
    },
    watch: {
        // console.log(this.province+this.city+this.district);
    }
  }
</script>

<style lang="scss" scoped>
  .app-cont {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    box-sizing: border-box;
    .header {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 80px;
    }
    .main {
      flex: 1;
      position: relative;
      box-sizing: border-box;
      .main-container {
        position: absolute;
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        overflow: hidden;
      }
    }
    .footer {
      position: fixed;
      bottom: 0;
      left: 0;
      height: 92px;
      width: 100%;
    }
  }

  .tabbar {
    position: fixed;
    box-sizing: border-box;
    height: 94px;
    border-top: 1px solid #f3f3f3;
    color: #898d93;
  }

  .fenlei {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    overflow-y: auto;
    background-color: #f4f4f4;
    display: flex;
    flex-direction: column;
    ::-webkit-scrollbar {
      display: none;
    }
    .idcar{
      .addr_detail{
        width:100%;
        height: 150px;
        border:none;
        padding: 30px;
        font-size: 25px;
        box-sizing: border-box;
        resize: none;
      }
      .select {
        height: 90px;
        margin-top: 20px;
        line-height: 90px;
        background: #ffffff;
        span{
          font-size: 24px;
          margin-left: 27px;
        }
        i{
          width: 32px;
          height: 32px;
          background: url('../assets/images/ico_2@1x.png') no-repeat;
          background-size: 100%;
          float: right;
          margin: 30px;
        }
        .selected{
          background: url('../assets/images/ico_1@1x.png') no-repeat;
          background-size: 100% 100%;
        }
        .red{color: #ff0000}
      }
      .addPick{
        /*height: 130px;*/
        font-size: 26px;
        color: #3b3b43;
        padding: 30px 20px 30px 35px;
        span{
          display: inline-block;
          /*margin-top: 45px;*/
        }
        .citySelect{
          float: right;
        }
      }
    }
    .btn{
      width:100%;
      height: 120px;
      text-align: center;
      line-height: 120px;
      background: #f4f4f4;
      position: fixed;
      bottom: 0;
      .btn-next{
        display: inline-block;
        width: 580px;
        height: 74px;
        margin:0;
        border-radius: 35px;
        border: 1px solid #fe3519;
        color: #fe3519;
        text-align: center;
        line-height: 74px;
        font-size: 26px;
      }
    }
  }

</style>

<style lang="scss">
  a:hover, a:visited, a:link, a:active {
    text-decoration: none;
    outline: none;
  }
  .idcar{
    height: 870px;
    .mint-checklist{
      margin-top: 20px;
    }
    .mint-cell {
      border-bottom: 2px solid #f4f4f4;
      .mint-cell-wrapper {
        background: #ffffff;
        height: 92px;
        line-height: 92px;
        font-size: 26px;
        padding: 0px 30px 0px 34px;
        color: #3b3b43;
        .mint-cell-title {
          .mint-cell-text {
            font-size: 26px;
            color: #3b3b43;
          }

        }
        .mint-cell-value {
          ::-webkit-input-placeholder {
            text-align: right;
          }
          .mint-field-core {
            color: #a1a1a1;
            text-align: right;
          }
        }
      }
    }
    .mint-field-other{
      width: 10px;
      height: 92px;
      text-align: center;
      color: #3b3b43;
      padding-left: 4px;
    }
  }
  .mint-checklist-title{
    margin: 0;
  }

  .mint-cell-title{
    font-size: 26px;
    color: #3b3b43;
  }
  .mint-checklist-label{
    padding: 0;
  }
  .mint-checkbox-label{
    margin-left: 0;
  }
</style>
